package  com.syqc.sensor.fwdrev

import androidx.recyclerview.widget.GridLayoutManager
import autodispose2.autoDispose 
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.base.BaseSureActivity 
import com.syqc.choose.car.ChooseCarActivity
import com.syqc.choose.teamcar.TeamsCarsActivity
import com.syqc.comlib.custom.timer.CustomDatePicker
import com.syqc.comlib.custom.timer.DateFormatUtils
import com.syqc.comlib.rx.filterFast
import com.syqc.comlib.utils.ActivityResultUtil.launchAct
import com.syqc.comlib.utils.ActivityResultUtil.registerResultOk
import com.syqc.comlib.utils.startActivityTo
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityFwdRevBinding
import com.syqc.monitor.databinding.IncludeChooseCarTimeBinding 
import com.syqc.monitor.databinding.IncludeChooseFastTimeBinding
import com.syqc.adapter.ChooseTimeAdapter
import com.syqc.utils.DataUtil
import com.syqc.utils.Keys
import com.syqc.utils.NetDataUtil
import com.syqc.utils.TimeUtil
import com.syqc.utils.ToolsUtil

class FwdRevActivity : BaseSureActivity<ActivityFwdRevBinding>(ActivityFwdRevBinding::inflate) {
    private lateinit var mReportVB: IncludeChooseFastTimeBinding
    lateinit var mChooseVB: IncludeChooseCarTimeBinding
    private val mVM by lazy { FwdRevViewModel() }


    override fun initView() {
        super.initView()
        mReportVB = IncludeChooseFastTimeBinding.bind(binding.root)
        mChooseVB = IncludeChooseCarTimeBinding.bind(binding.root)
        ToolbarHelper.setToolbar(this, R.string.title_report_fwd_rev)
        mReportVB.recyclerWrap.layoutManager = GridLayoutManager(this, 4)
        mReportVB.recyclerWrap.adapter = mAdapter.apply {
            setNewInstance(mVM.timeList)
            setOnItemClickListener { _, _, position ->
                index = position
                notifyDataSetChanged()
                mVM.chooseTime(position)
            }
        }
        initData()
        initIntent()
    }

    override fun setListener() {
        mChooseVB.lrvChooseCar.rightClick {
            if (NetDataUtil.isSyncTree){
                launcher.launchAct<TeamsCarsActivity>(this)
            }else{
                launcher.launchAct<ChooseCarActivity>(this)
            }
        }

        mChooseVB.lrvStartTime.rightClick {
            mVM.isStart = true
            mDatePicker.show(mChooseVB.lrvStartTime.getRightText())
        }
        mChooseVB.lrvEndTime.rightClick {
            mVM.isStart = false
            mDatePicker.show(mChooseVB.lrvEndTime.getRightText())
        }

        mSureVB.tvSure.filterFast {
            if (ToolsUtil.isFastClick()) return@filterFast
            val startTime = mChooseVB.lrvStartTime.getRightText()
            val endTime = mChooseVB.lrvEndTime.getRightText()
            mVM.isParamOk(startTime, endTime)
        }

        binding.cbFwd.setOnClickListener {
            mVM.isFwd = binding.cbFwd.isChecked
        }
        binding.cbRev.setOnClickListener {
            mVM.isRev = binding.cbRev.isChecked
        }
        binding.cbStop.setOnClickListener {
            mVM.isStop = binding.cbStop.isChecked
        }

    }

    fun initData() {
        mVM.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)
        mVM.startObserve().autoDispose(scopeProvider).subscribe {
            mChooseVB.lrvStartTime.setRightText(it)
        }

        mVM.endObserve().autoDispose(scopeProvider).subscribe {
            mChooseVB.lrvEndTime.setRightText(it)
        }
        mVM.dataLive.observe(this) {
            DataUtil.fwdrevs = it
            startActivityTo<FwdRevInfoActivity>(this)
        }

    }

    private fun initIntent() {
        val carId = intent.getStringExtra(Keys.INTENT_CAR_ID)
        val carName = intent.getStringExtra(Keys.INTENT_PLATE) ?: ""
        if (carId == null) return
        mVM.carId = carId
        mVM.carName = carName
        mChooseVB.lrvChooseCar.setRightText(carName)
        mChooseVB.lrvChooseCar.isEnabled = false
    }

    private val mAdapter: ChooseTimeAdapter by lazy { ChooseTimeAdapter() }

    private val mDatePicker: CustomDatePicker by lazy {
        CustomDatePicker(
            this,
            callback,
            TimeUtil.normalDate,
            mVM.nowTimeFormat(),
            !mVM.isSimple
        ).apply {
            setCanShowPreciseTime(!mVM.isSimple)
        }
    }


    private val callback: CustomDatePicker.Callback = object : CustomDatePicker.Callback {
        override fun onTimeSelected(timestamp: Long) {
            mAdapter.index = -1
            mAdapter.notifyDataSetChanged()
            val time: String = DateFormatUtils.long2Str(timestamp, !mVM.isSimple)

            if (mVM.isStart) {
                mChooseVB.lrvStartTime.setRightText(time)
            } else {
                mChooseVB.lrvEndTime.setRightText(time)
            }
            mDatePicker.dismiss()
        }
    }

    private val launcher = registerResultOk { result ->
        result.data?.let {
            val carId = it.getStringExtra(Keys.INTENT_CAR_ID) ?: ""
            val carName = it.getStringExtra(Keys.INTENT_CAR_NAME) ?: ""
            mChooseVB.lrvChooseCar.setRightText(carName)
            mVM.carName = carName
            mVM.carId = carId
        }
    }
}